package webx.actions;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
import org.eclipse.ui.internal.WorkbenchImages;
import org.eclipse.ui.internal.registry.EditorDescriptor;
import org.eclipse.ui.part.FileEditorInput;

import sssex.editors.MultiPageEditor;

@SuppressWarnings("restriction")
public class OpenFileInnerRunnable extends Test implements Runnable {
	
	String pagepath = ""; 
	
	int row = -1;
	
	public OpenFileInnerRunnable() {
	}
	
	public OpenFileInnerRunnable(String pagepath,String row ) {
		this.pagepath = pagepath;
		if(row!=null && !"".equals(row)){
			try{
				this.row = Integer.valueOf(row);
			}catch(Throwable e){}
		}
	}
	public static void main(String[]args){
		System.out.println(null instanceof Test );
	}
	
	private class Action1 extends Action{

        public Action1() {

            ImageDescriptor img = WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_DTOOL_NEW_FASTVIEW);

            setHoverImageDescriptor(img);

            setText("Action1");

        }

       

        @Override

        public void run() {

            // TODO 自动生成的方法存根

            super.run();

        }

    }
	
	public void run() {
		try
		{   
//			IEditorPart editor = page.getActiveEditor();
//			// System.out.println("getActiveEditor:"+editor+"; "+(editor instanceof sssex.editors.MultiPageEditor));
//			if(editor instanceof sssex.editors.MultiPageEditor){
//				// com.genuitec.eclipse.webdesigner3.editors.WebDesignerMultiPageEditor pageEdtior
//				((sssex.editors.MultiPageEditor)editor).getEditor().getSourceEditor().getTextViewer().doOperation(org.eclipse.wst.sse.ui.internal.StructuredTextViewer.UNDO);
//				IDocument doc = ((sssex.editors.MultiPageEditor)editor).doc;
//				System.out.println("docq:"+doc.getClass());
//				if (doc instanceof IStructuredDocument){
//					System.out.println("doc:");
//				}
//			}
			IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			if( page.getActiveEditor() instanceof MultiPageEditor ){
				MultiPageEditor editor = (MultiPageEditor)page.getActiveEditor();
				IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(pagepath));
				//IDE.openEditor(page, input);
				IEditorPart[] parts = editor.findEditors(new FileEditorInput(file));
				if(parts.length>0){
					editor.setActiveEditor(parts[0]);
				}else{
					EditorDescriptor ded = (EditorDescriptor)IDE.getDefaultEditor(file);
					IEditorPart etiorPart=ded.createEditor();
					//com.genuitec.eclipse.webdesigner3.editors.WebDesignerMultiPageEditor pageEdtior = new com.genuitec.eclipse.webdesigner3.editors.WebDesignerMultiPageEditor();
					int index = editor.addPage(etiorPart, new FileEditorInput(file) );
					//etiorPart.getEditorSite().registerContextMenu(menuManager, selectionProvider)
					etiorPart.getEditorSite().getActionBars().getToolBarManager().add(new Action1());
					editor.setPageText1(index, etiorPart.getTitle());
				}
				// editor.addPage(index, control);
				//System.out.println("sssss:"+index+";"+).length);
			}
		}catch (Throwable e){
			e.printStackTrace();
		}
	}
	
}
